package com.atguigu.day03;

import com.atguigu.beans.WaterSensor;
import com.atguigu.func.MyMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/4/1
 * 该案例演示了转换算子-map
 * 需求：提取WaterSensor中的id字段的功能
 */
public class Flink09_Trans_Map {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //TODO 2.准备数据
        DataStreamSource<WaterSensor> wsDS
                = env.fromElements(new WaterSensor("ws1", 10L, 10), new WaterSensor("ws2", 20L, 20));
        //TODO 3.从流中的WaterSensor对象中提取传感器id
        //匿名内部类方式创建处理函数对象
        /*SingleOutputStreamOperator<String> idDS = wsDS.map(
                new MapFunction<WaterSensor, String>() {
                    @Override
                    public String map(WaterSensor ws) throws Exception {
                        return ws.getId();
                    }
                }
        );
        //lambda表达式
        SingleOutputStreamOperator<String> idDS = wsDS.map(
                ws -> ws.getId()
        );

        //方法的引用
        //SingleOutputStreamOperator<String> idDS = wsDS.map(WaterSensor::getId);
        */
        //单独封装函数的实现类
        SingleOutputStreamOperator<String> idDS = wsDS.map(new MyMapFunction());
        //TODO 4.打印
        idDS.print();
        //TODO 5.提交作业
        env.execute();
    }
}
